/* 
 * The MIT License
 *
 * Copyright 2014 Venkat Ram Akkineni.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.tfsnb.core.multiview;

import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
//@ConvertAsProperties(
//        dtd = "-//org.netbeans.modules.tfsnb.core//TFSConnectionExplorer//EN",
//        autostore = false
//)
//@TopComponent.Description(
//        preferredID = "TFSConnectionExplorerTopComponent",
//        iconBase = "org/netbeans/modules/tfsnb/core/TeamFoundationServer.gif",
//        persistenceType = TopComponent.PERSISTENCE_ALWAYS
//)
//@TopComponent.Registration(mode = "editor", openAtStartup = false, position = 1000)
//@ActionID(category = "Window", id = "org.netbeans.modules.tfsnb.core.TFSConnectionExplorerTopComponent")
//@ActionReference(path = "Menu/Window" /*, position = 333 */)
//@TopComponent.OpenActionRegistration(
//        displayName = "#CTL_TFSConnectionExplorerAction",
//        preferredID = "TFSConnectionExplorerTopComponent"
//)
//@Messages({
//    "CTL_TFSConnectionExplorerAction=TFSConnectionExplorer",
//    "CTL_TFSConnectionExplorerTopComponent=TFSConnectionExplorer Window",
//    "HINT_TFSConnectionExplorerTopComponent=This is a TFSConnectionExplorer window"
//})
@Deprecated
public final class TFSConnectionExplorerTopComponent1 extends TopComponent {
    
    public TFSConnectionExplorerTopComponent1() {
        initComponents();
        try {
            multiViewManager = new MultiViewManager(this);
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, ex.getMessage());
        }
        setupMultiViewComponents();
//        setName(Bundle.CTL_TFSConnectionExplorerTopComponent());
//        setToolTipText(Bundle.HINT_TFSConnectionExplorerTopComponent());
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        headerPanel = new javax.swing.JPanel();
        bodyPanel = new javax.swing.JPanel();
        bodyLayeredPane = new javax.swing.JLayeredPane();

        setLayout(new java.awt.BorderLayout());

        headerPanel.setMaximumSize(new java.awt.Dimension(25, 28));
        headerPanel.setMinimumSize(new java.awt.Dimension(25, 28));
        headerPanel.setPreferredSize(new java.awt.Dimension(25, 28));

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 588, Short.MAX_VALUE)
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 28, Short.MAX_VALUE)
        );

        add(headerPanel, java.awt.BorderLayout.PAGE_START);

        javax.swing.GroupLayout bodyLayeredPaneLayout = new javax.swing.GroupLayout(bodyLayeredPane);
        bodyLayeredPane.setLayout(bodyLayeredPaneLayout);
        bodyLayeredPaneLayout.setHorizontalGroup(
            bodyLayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 564, Short.MAX_VALUE)
        );
        bodyLayeredPaneLayout.setVerticalGroup(
            bodyLayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 262, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout bodyPanelLayout = new javax.swing.GroupLayout(bodyPanel);
        bodyPanel.setLayout(bodyPanelLayout);
        bodyPanelLayout.setHorizontalGroup(
            bodyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 588, Short.MAX_VALUE)
            .addGroup(bodyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bodyPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(bodyLayeredPane)
                    .addContainerGap()))
        );
        bodyPanelLayout.setVerticalGroup(
            bodyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 286, Short.MAX_VALUE)
            .addGroup(bodyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bodyPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(bodyLayeredPane)
                    .addContainerGap()))
        );

        add(bodyPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
    
    private void setupMultiViewComponents() {
        addViews();
        addSwitchViewToggleButtons();
        multiViewManager.showFirstPanel();
    }    
    
    private void addViews() {
        Map<String, MultiViewPanel> views = multiViewManager.getViews(true);
        for(MultiViewPanel view: views.values()) {
            JComponent visualRepresentation = view.getVisualRepresentation();
            int position = view.getMultiViewPanelDescription().getPosition();
//            visualRepresentation.putClientProperty(JLayeredPane.LAYER_PROPERTY, position);
            bodyLayeredPane.add(new JPanel(), position);
//            bodyLayeredPane.add(view.getVisualRepresentation());
        }
    }
    
    private void addSwitchViewToggleButtons() {
        switchViewButtons = multiViewManager.getSwitchViewButtons(true);
        GroupLayout headerLayout = new GroupLayout(headerPanel);
        headerPanel.setLayout(headerLayout);
        
        if(switchViewButtons != null && switchViewButtons.size() > 0) {
            // Horizontal Group
            SequentialGroup horizontalSequentialGroup = headerLayout.createSequentialGroup();           
            for(JToggleButton button: switchViewButtons.values()) {
                horizontalSequentialGroup
                        .addComponent(button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);                                     
            }
//            horizontalSequentialGroup.addComponent(multiViewToolBar, GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE);
            headerLayout.setHorizontalGroup(headerLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(horizontalSequentialGroup));
            
//            headerPanelLayout.setHorizontalGroup(
//                    headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//                    .addGroup(headerPanelLayout.createSequentialGroup()
//                            .addComponent(jToggleButton1)
//                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
//                            .addComponent(jToggleButton2)
//                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
//                            .addComponent(jToggleButton3)
//                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
//                            .addComponent(jToggleButton4)
//                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
//                            .addComponent(multiViewToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
//            );
            
            // Vertical Group
            ParallelGroup verticalGroup = headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false);
//            verticalGroup.addComponent(multiViewToolBar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);
            for(JToggleButton button: switchViewButtons.values()) {
                verticalGroup.addComponent(button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);
            }
//            headerLayout.setVerticalGroup(
//                headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//                .addGroup(headerLayout.createSequentialGroup()
//                    .addGroup(headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
//                        .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
//                        .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
//                        .addComponent(jToggleButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
//                        .addComponent(jToggleButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
//                        .addComponent(jToggleButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
//                    .addContainerGap())
//            );
            headerLayout.setVerticalGroup(
                    headerLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addGroup(headerLayout.createSequentialGroup().addGroup(verticalGroup).addContainerGap()));
        }
    }

    public JLayeredPane getBodyLayeredPane() {
        return bodyLayeredPane;
    }
   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLayeredPane bodyLayeredPane;
    private javax.swing.JPanel bodyPanel;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel headerPanel;
    // End of variables declaration//GEN-END:variables
    private final ArrayList<Class> panelClasses = new ArrayList<>();
    private Map<String, SwitchViewToggleButton> switchViewButtons = new TreeMap<>();
    private LayoutManager jPanel1Layout;
    private MultiViewManager multiViewManager;
    private static final Logger LOG = Logger.getLogger(TFSConnectionExplorerTopComponent1.class.getName());
    
}
